package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.model.EbCardInfo;
import cn.com.chengmandian.bike.model.EbMemberCard;
import cn.com.chengmandian.bike.model.EbMemberInfo;
import cn.com.chengmandian.bike.model.EbRedeemCode;
import cn.com.chengmandian.bike.mapper.EbRedeemCodeMapper;
import cn.com.chengmandian.bike.service.IEbCardInfoService;
import cn.com.chengmandian.bike.service.IEbMemberCardService;
import cn.com.chengmandian.bike.service.IEbMemberInfoService;
import cn.com.chengmandian.bike.service.IEbRedeemCodeService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 电单车运营-兑换码 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2023-10-18
 */
@Service
public class EbRedeemCodeServiceImpl extends ServiceImpl<EbRedeemCodeMapper, EbRedeemCode> implements IEbRedeemCodeService {

    @Autowired
    private IEbMemberInfoService memberInfoService;
    @Autowired
    private IEbCardInfoService cardInfoService;
    @Autowired
    private IEbMemberCardService memberCardService;

    @Override
    public EbRedeemCode getByCode(String code) {
        return this.getOne(new LambdaQueryWrapper<EbRedeemCode>()
                .eq(EbRedeemCode::getCode, code)
                .eq(EbRedeemCode::getIsDeleted, CommonConstant.ZERO)
                .eq(EbRedeemCode::getIsEnabled, CommonConstant.ONE)
                .last("limit 0,1"));
    }

    @Override
    public void checkRedeemCode(String code, Integer areaId, Integer memberId) {
        EbRedeemCode redeemCode = this.getByCode(code);
        Assert.notNull(redeemCode, "兑换码不存在或已失效");
        Assert.isTrue(redeemCode.getAllTimes() >= redeemCode.getUseTimes(), "该兑换码兑换次数已达上限");
        if(redeemCode.getExpireTime() != null){
            Assert.isTrue(redeemCode.getExpireTime().getTime() > DateUtil.date().getTime(), "该兑换码已过期");
        }

        EbMemberInfo memberInfo = memberInfoService.getById(memberId);
        Assert.isTrue(memberInfo != null && StrUtil.isNotBlank(memberInfo.getMobile()), "用户不支持兑换");
        if(StrUtil.isNotBlank(redeemCode.getLimitPhone())) {
            List<String> limitPhones = Arrays.asList(redeemCode.getLimitPhone().split(","));
            Assert.isTrue(limitPhones.contains(memberInfo.getMobile()), "用户不支持兑换");
        }

        // 骑行卡
        if(redeemCode.getType().equals(CommonConstant.ONE)){
            EbCardInfo ebCardInfo = cardInfoService.getById(redeemCode.getTypeId());
            Assert.isTrue(ebCardInfo.getIsDeleted().equals(CommonConstant.ZERO), "骑行卡已删除，不可兑换");
            Assert.isTrue(ebCardInfo.getAreaId().equals(areaId), "兑换码不支持使用");

            EbMemberCard memberCard = memberCardService.getByRedeemCode(memberId, redeemCode.getId());
            Assert.isNull(memberCard, "兑换码已使用");
        }
    }

    @Override
    public void useRedeemCode(String code, Integer memberId) {
        EbRedeemCode redeemCode = this.getByCode(code);

        // 骑行卡
        if(redeemCode.getType().equals(CommonConstant.ONE)){
            EbCardInfo ebCardInfo = cardInfoService.getById(redeemCode.getTypeId());
            memberCardService.createCard(null, memberId, ebCardInfo.getId(), BigDecimal.ZERO, CommonConstant.TWO, redeemCode.getId());
        }

        // 次数加一
        baseMapper.costTimes(redeemCode.getId(), CommonConstant.ONE);
    }
}
